from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView, GenericAPIView
from .models import SKU, GoodsCategory
from .serializers import SKUSerializer, SKUIndexSerializer
from drf_haystack.viewsets import HaystackViewSet


class SKUListView(ListAPIView):
    """商品列表数据查询"""

    serializer_class = SKUSerializer
    filter_backends = [OrderingFilter]  # 指定过滤后端为排序过滤
    ordering_fields = [
        "create_time",
        "price",
        "sales",
    ]  # 指定排序字段 (查询所有数据接口 中查的是那个模型中的数据,里面就指定那个模型的字段)

    def get_queryset(self):
        """如果当前在视图中没有去定义get /post方法 那么就没法定义一个参数用来接收正则组提取出来的url路径参数, 可以利用视图对象的 args或kwargs属性去获取啊"""
        category_id = self.kwargs.get("category_id")  # 获取到category_id
        return SKU.objects.filter(is_launched=True, category_id=category_id)


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """

    index_models = [SKU]

    serializer_class = SKUIndexSerializer
